package cc.rengu.redp.bizimpl.igas.service.impl;

import cc.rengu.redp.bizimpl.igas.dao.MchntOrderInfoMapper;
import cc.rengu.redp.bizimpl.igas.domain.MchntOrderInfo;
import cc.rengu.redp.bizimpl.igas.domain.SUserInfo;
import cc.rengu.redp.bizimpl.igas.service.IMchntOrderInfoService;
import cc.rengu.redp.bizimpl.igas.vo.MchntOrderInfoVO;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserInfoService;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.common.utils.SortUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author guohaocong
 */
@Service
@Slf4j
public class MchntOrderInfoServiceImpl extends ServiceImpl<MchntOrderInfoMapper, MchntOrderInfo> implements IMchntOrderInfoService {

    @Autowired
    private IUpmpUserInfoService upmpUserInfoService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Override
    public IPage<MchntOrderInfoVO> findByPage(QueryRequest request, MchntOrderInfoVO mchntOrderInfoVO, String userId) throws RedpException {
        UpmpUserInfo userInfo = upmpUserInfoService.findById(userId);
        if (mchntOrderInfoVO.getInstId() == null) {
            mchntOrderInfoVO.setInstId(userInfo.getInstId());
        }
        //先page执行
        if (StringUtils.isNotEmpty(mchntOrderInfoVO.getTxnName())) {
            String instId = mchntOrderInfoVO.getInstId();
            String txnName = mchntOrderInfoVO.getTxnName();
            List<Map> txnLst = null;
            try {
                txnLst = this.baseMapper.selectTxnInfoByNumAndName(instId, txnName);
            } catch (Exception e) {
                String message = "查询TxnName数据失败";
                log.info(message);
                throw new RedpException(message);
            }
            String msgSrcId = "";
            String transChannelId = "";
            String bizType = "";
            for (int i = 0; i < txnLst.size(); i++) {
                msgSrcId += ",'" + (String) txnLst.get(i).get("msgSrcId") + "'";
                transChannelId += ",'" + (String) txnLst.get(i).get("transChannelId") + "'";
                bizType += ",'" + (String) txnLst.get(i).get("bizType") + "'";
            }
            mchntOrderInfoVO.setMsgSrcIdList(msgSrcId.substring(1));
            mchntOrderInfoVO.setTransChannelIdList(transChannelId.substring(1));
            mchntOrderInfoVO.setBizTypeList(bizType.substring(1));
        }
        List<MchntOrderInfoVO> list = this.baseMapper.findByPage(mchntOrderInfoVO);
        Page<MchntOrderInfoVO> page = new Page<>();
        page.setRecords(list);
        SortUtil.handlePageSort(request, page, true);
        return page;

    }

    @Override
    public Map<String, Object> getUserInfoList(QueryRequest request, String q) {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        String userOrgCodes = user.getOrgCode();
        q = q.replaceAll("&amp;", "&");
        LambdaQueryWrapper<UpmpUserInfoVO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (null == q) {
            return null;
        }
        String instId = "";
        String param1 = null;
        String[] param = q.split("&");
        if (null != param && param.length > 0) {
            param1 = param[0];
        }
        if (null != param && param.length > 1) {
            if (Common.isBlank(param[1]) && Common.isNotBlank(user.getInstId())) {
                instId = user.getInstId();
            } else {
                if (user.isAdmin()) {
                    instId = param[1];
                } else {
                    instId = user.getInstId();
                }
            }
        } else {
            instId = user.getInstId();
        }
        String mchntNo = "";
        if (null != param && param.length > 2) {
            if (Common.isNotBlank(param[2])) {
                mchntNo = param[2];
            }
        }
        List<SUserInfo> list1 = this.baseMapper.findUserInfoList(instId, mchntNo, param1);
        Map<String, Object> maps = new HashMap<String, Object>();
        Map<String, String> map = new HashMap<String, String>();
        List<Object> list = new ArrayList<Object>();
        Page<SUserInfo> page = new Page();
        Page<SUserInfo> page1 = page.setRecords(list1);
        if (list1 != null) {
            for (int i = 0; i < list1.size(); i++) {
                map = new HashMap<String, String>();
                SUserInfo info = list1.get(i);
                map.put("name",  info.getNickName());
                map.put("id",  info.getUserId());
                list.add(map);
            }
            maps.put("results", list);
            maps.put("total", page.getTotal());
            int pageIndex = Common.isBlank(request.getPageNum()) ? 1 : request.getPageNum();
            if (pageIndex == page1.getTotal()) {
                maps.put("more", false);
            } else {
                maps.put("more", true);
            }
        }
        return maps;
    }

}
